/*
 *     SocialLedge.com - Copyright (C) 2013
 *
 *     This file is part of free software framework for embedded processors.
 *     You can use it and/or distribute it as long as this copyright header
 *     remains unmodified.  The code is free for personal use and requires
 *     permission to use in a commercial product.
 *
 *      THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
 *      OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
 *      MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
 *      I SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR
 *      CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.
 *
 *     You can reach the author of this software at :
 *          p r e e t . w i k i @ g m a i l . c o m
 */

/**
 * @file
 * @brief This is the application entry point.
 */
#include <stdio.h>      // printf() / scanf()
#include <string.h>     // strcmp
#include "io.hpp"       // Board sensors
#include "utilities.h"  // delay_ms()
#include "uart2.hpp"    // Uart 2 for LCD
#include "uart3.hpp"    // Uart 3 for Xbee


/**
 * Initializes the Xbee
 * @param xbee         Your Xbee class
 * @param srcAddr      Source address of YOUR xbee
 * @param dstAddr      Destination addres of the OTHER xbee
 * @param networkAddr  Personal Network ID of BOTH of your xbees
 */
bool xbee_init(UartIface& xbee, short srcAddr, short dstAddr, short networkID);

int main(void)
{
    /**
     * Initialize our LCD screen.
     * LCDs RX pin should be connected to our Uart2 Tx Pin
     * On SJOne Board, it is mis-labeled as "Uart1", hook it up to the "TXo" pin.
     */
    Uart2& lcd = Uart2::getInstance();
    lcd.init(38400, 8, 128);
    lcd.putChar(0xF0);
    delay_ms(100);

    /**
     * Initialize communication for Wireless Xbee
     */
    puts("Initializing Xbee...");
    Uart3& xbee = Uart3::getInstance();
    xbee.init(9600, 128, 128);

    /**
     * TODO Change your network, source and destination addresses so your data
     * will not collide with others
     */
    short network_id = 0x1337;   /* Netowrk ID */
    short my_addr    = 0x6200;   /* OUR address */
    short dst_addr   = 0x6201;   /* The other Xbee's address (destination) */

    if (!xbee_init(xbee, my_addr, dst_addr, network_id)) {
        puts("Failed to initialize Xbee");
    }
    else {
        puts("Xbee initialized!");
    }




    /**
     * Read the serialLCDUserManual.pdf for LCD commands.  It is in your project folder.
     * In the example below, we are using an LCD command to send data to its 1st line.
     */

    int val_light = 0;
    bool sw_1;
    bool sw_2;
    bool sw_3;
    bool sw_4;
    bool sw_5;
    char letter;

    lcd.printf("$BLIGHT:%i\n", 75);
    while(1)
    {
        lcd.printf("$L:0:Hello World!!!!! \n");
        lcd.printf("$L:1:I know my ABC's\n");
        for(int i = 65; i<=90; i++){
            letter = i;
            lcd.printf("$L:2:%c\n", letter);
            sw_1 = SW.getSwitch(1);
            if (sw_1){
                continue;
            }
            delay_ms(1000);
        }

        sw_1 = SW.getSwitch(1);
        while (sw_1){
            lcd.printf("$CLR_SCR\n");
            lcd.printf("$BLIGHT:%i\n", 5);
            sw_1 = SW.getSwitch(1);
            }
        sw_2 = SW.getSwitch(2);
        while (sw_2){
            val_light += 5;
            lcd.printf("$BLIGHT:%i\n", val_light);
            delay_ms(20);
            sw_2 = SW.getSwitch(2);
        }
        sw_3 = SW.getSwitch(3);
        while (sw_3){
             val_light -= 5;
             lcd.printf("$BLIGHT:%i\n", val_light);
             delay_ms(20);
             sw_3 = SW.getSwitch(3);
                }




        /**
         * TODO Your objective is to take data from xbee and display it on the LCD.
         * Here is an example of how to read a line from xbee :
         * Note that the other side must send a full line and it MUST HAVE \n character.
         */
        char line[128] = { 0 };
        bool rxOk = xbee.gets(line, sizeof(line), 1000);
        if (rxOk) {
            printf("Received from Xbee: %s\n", line);
        }
    }

    return -1;
}

bool xbee_print_response(UartIface& xbee, const char *message)
{
    char response[128] = { 0 };

    printf("%14s : ", message);
    if (!xbee.gets(response, sizeof(response), 2000)) {
        printf("Error (%s) - %i\n", response, strlen(response));
        return false;
    }
    else {
        printf("%s\n", response);
        return true;
    }
}

bool xbee_init(UartIface& xbee, short srcAddr, short dstAddr, short networkID)
{
    delay_ms(1100);
    xbee.put("+++");
    xbee_print_response(xbee, "Xbee Cmd Mode");

    xbee.printf("ATID%04X\r\n", networkID);
    xbee_print_response(xbee, "Set Net ID");

    /* Destination address */
    xbee.printf("ATDH0000\r\n");
    xbee_print_response(xbee, "Set Dst ID1");
    xbee.printf("ATDL%04X\r\n", dstAddr);
    xbee_print_response(xbee, "Set Dst ID2");

    /* Our address */
    xbee.printf("ATMY%04X\r\n", srcAddr);
    xbee_print_response(xbee, "Set MY Addr");

    /* Write memory */
    xbee.printf("ATWR\r\n");
    xbee_print_response(xbee, "Xbee Save");

    /* Exit command mode */
    xbee.printf("ATCN\r\n");
    xbee_print_response(xbee, "Xbee Exit");

    return true;
}
